package com.xindaoapp.happypet.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.AreaInfo;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.happypet.utils.KeyValuePair;

public class ChooseAreaDialog extends Dialog {
	private List<AreaInfo> list = new ArrayList<AreaInfo>();
	/** 区 */
	private ListView arealistView;
	/** 市 */
	private ListView citylistView;
	/** 省 */
	private ListView proviencelistView;

	private View lastStep;
	private View next;
	private int layer;

	private ChooseCityAdapter firstAdapter;
	private ChooseCityAdapter secondAdapter;
	private ChooseCityAdapter thirdAdapter;
	private IRequest<KeyValuePair<int[], String[]>> iRequest;

	private Context mContext;

	public ChooseAreaDialog(Context context) {
		super(context);
	}

	public ChooseAreaDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
	}

	public ChooseAreaDialog(Context context, int theme, List<AreaInfo> list,
			IRequest<KeyValuePair<int[], String[]>> iRequest) {
		super(context, theme);
		mContext = context;
		this.list = list;
		this.iRequest = iRequest;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_choose_city);
		arealistView = (ListView) findViewById(R.id.listview_area);
		citylistView = (ListView) findViewById(R.id.listview_city);
		proviencelistView = (ListView) findViewById(R.id.listview_provience);
		proviencelistView.setAdapter(firstAdapter = new ChooseCityAdapter(list));
		lastStep = findViewById(R.id.last_step);
		next = findViewById(R.id.next);
		findViewById(R.id.close).setOnClickListener(mOnClickListener);
		lastStep.setOnClickListener(mOnClickListener);
		next.setOnClickListener(mOnClickListener);
		setLayer(0, true);
	}

	private final View.OnClickListener mOnClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.last_step:
				setLayer(--layer, false);
				break;
			case R.id.next:
				setLayer(++layer, true);
				break;
			case R.id.close:
				dismiss();
				break;
			default:
				break;
			}
		}
	};

	/**
	 * 设置view的层级
	 */
	private void setLayer(int layer, boolean forward) {
		switch (layer) {
		case 0:// 第一层
			layer = 0;
			lastStep.setVisibility(View.GONE);
			next.setVisibility(View.VISIBLE);
			proviencelistView.setVisibility(View.VISIBLE);
			citylistView.setVisibility(View.GONE);
			arealistView.setVisibility(View.GONE);
			break;
		case 1:// 第二层
			layer = 1;

			// 区分前进还是返回

			if (forward) {
				citylistView.setAdapter(secondAdapter = new ChooseCityAdapter(((AreaInfo) firstAdapter
						.getItem(firstAdapter.getCurrentSelectedPosition())).list));
			}

			lastStep.setVisibility(View.VISIBLE);
			next.setVisibility(View.VISIBLE);
			proviencelistView.setVisibility(View.GONE);
			citylistView.setVisibility(View.VISIBLE);
			arealistView.setVisibility(View.GONE);
			break;
		case 2:// 第三层
			layer = 2;

			List<AreaInfo> list = ((AreaInfo) secondAdapter.getItem(secondAdapter
					.getCurrentSelectedPosition())).list;

			if (list.size() > 0) {
				arealistView.setAdapter(thirdAdapter = new ChooseCityAdapter(list));

				lastStep.setVisibility(View.VISIBLE);
				next.setVisibility(View.VISIBLE);
				proviencelistView.setVisibility(View.GONE);
				citylistView.setVisibility(View.GONE);
				arealistView.setVisibility(View.VISIBLE);
			} else {
				/*iRequest.request((AreaInfo) secondAdapter.getItem(secondAdapter
						.getCurrentSelectedPosition()));
				dismiss();*/
			}
			break;
		case 3:// 退出第三层
				// (AreaInfo)
				// thirdAdapter.getItem(thirdAdapter.getCurrentSelectedPosition())
			int[] id = new int[3];
			id[0] = ((AreaInfo) firstAdapter.getItem(firstAdapter.getCurrentSelectedPosition())).id;
			id[1] = ((AreaInfo) secondAdapter.getItem(secondAdapter.getCurrentSelectedPosition())).id;
			id[2] = ((AreaInfo) thirdAdapter.getItem(thirdAdapter.getCurrentSelectedPosition())).id;
			String[] str = new String[3];
			str[0] = ((AreaInfo) firstAdapter.getItem(firstAdapter.getCurrentSelectedPosition())).name;
			str[1] = ((AreaInfo) secondAdapter.getItem(secondAdapter.getCurrentSelectedPosition())).name;
			str[2] = ((AreaInfo) thirdAdapter.getItem(thirdAdapter.getCurrentSelectedPosition())).name;
			iRequest.request(new KeyValuePair<int[], String[]>(id, str));
			dismiss();
			break;
		default:
			break;
		}
	}

	class ChooseCityAdapter extends BaseAdapter {
		List<AreaInfo> list;

		int currentSelected = 0;

		public ChooseCityAdapter(List<AreaInfo> list) {
			super();
			this.list = list;
		}

		public int getCurrentSelectedPosition() {
			return currentSelected;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_choose_city, null);
				holder.rb = (TextView) convertView.findViewById(R.id.radio_button);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			AreaInfo info = list.get(position);
			holder.rb.setText(info.name);
			if (currentSelected == position) {
				holder.rb.setSelected(true);
			} else {
				holder.rb.setSelected(false);
			}
			holder.rb.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					if (currentSelected == position) {} else {
						currentSelected = position;
					}
					notifyDataSetChanged();
				}
			});
			convertView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					if (currentSelected == position) {} else {
						currentSelected = position;
					}
					notifyDataSetChanged();
				}
			});
			return convertView;
		}

		class ViewHolder {
			TextView rb;
		}

	}

}
